﻿using Newtonsoft.Json;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HtFileSync
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            executeRadioButton.Checked = true;
            timer1.Stop();
            timer2.Stop();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" & textBox2.Text != "")
            {
                if (button1.Text == "启动")
                {
                    button1.Text = "停止";
                    button1.ForeColor = Color.FromArgb(188, 0, 0);
                    if (executeRadioButton.Checked)
                    {
                        PerformTasks();
                        button1.Text = "启动";
                        button1.ForeColor = Color.FromArgb(0, 180, 0);
                        toolStripStatusLabel1.Text = "启动";
                    }
                    if (intervalRadioButton.Checked)
                    {
                        int dd = int.Parse(textBoxDay.Text); //日
                        int hh = int.Parse(textBoxHour.Text); //时
                        int mm = int.Parse(textBoxMinute.Text); //分
                        int ss = int.Parse(textBoxSecond.Text); //秒
                        timer1.Interval = (((dd * 24 + hh) * 60 + mm) * 60 + ss) * 1000;
                        timer1.Start();
                        toolStripStatusLabel1.Text = "正在工作";
                    }
                    if (everydayRadioButton.Checked)
                    {
                        timer2.Start();
                        toolStripStatusLabel1.Text = "正在工作";
                    }

                }
                else
                {
                    button1.Text = "启动";
                    button1.ForeColor = Color.FromArgb(0, 180, 0);
                    timer1.Stop();
                    timer2.Stop();
                    toolStripStatusLabel1.Text = "已停止工作";
                }
            }


        }

        public void PerformTasks()
        {
            switch (workMode.Text)
            {
                case "整体复制到目标文件夹":
                    if (includeSubfolders.Checked)
                    {
                        CopyFiles.CopyDir(textBox1.Text, textBox2.Text);

                    }
                    else
                    {
                        CopyFiles.CopyFolder(textBox1.Text, textBox2.Text);
                    }

                    break;

                case "增量复制到目标文件夹":
                    try
                    {
                        if (deleteOldFileCheck.Checked & delOldDays.Text != "") //先删除旧文件
                        {
                            int dd = int.Parse(delOldDays.Text);
                            if (dd > 0)
                            {
                                CopyFiles.DeleteOldFile(dd, textBox2.Text);
                            }

                        }
                    }
                    catch (Exception e)
                    {
                        toolStripStatusLabel1.Text = e.Message;
                    }
                    //调用文件备份程序
                    CopyFiles.FileBackupC(textBox1.Text, textBox2.Text, onlyExtensions.Checked, onlyExtensionsText.Text, zipFileCheck.Checked, zipFormat.Text, includeSubfolders.Checked);

                    break;

                case "增量备份并保留老文件":
                    try
                    {
                        if (deleteOldFileCheck.Checked & delOldDays.Text != "") //先删除旧文件
                        {
                            int dd = int.Parse(delOldDays.Text);
                            if (dd > 0)
                            {
                                CopyFiles.DeleteOldFile(dd, textBox2.Text);
                            }

                        }
                    }
                    catch (Exception e)
                    {
                        toolStripStatusLabel1.Text = e.Message;
                    }
                    //调用文件备份程序
                    CopyFiles.FileBackupD(textBox1.Text, textBox2.Text, onlyExtensions.Checked, onlyExtensionsText.Text, zipFileCheck.Checked, zipFormat.Text, includeSubfolders.Checked);

                    break;

                default:
                    break;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件夹路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (dialog.SelectedPath.Trim() != "")
                {
                    textBox1.Text = dialog.SelectedPath.Trim();
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件夹路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (dialog.SelectedPath.Trim() != "")
                {
                    textBox2.Text = dialog.SelectedPath.Trim();
                }

            }
        }




        //“显示窗体”单击事件
        private void 显示窗体_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        //“隐藏窗体”单击事件
        private void 隐藏窗体_Click(object sender, EventArgs e)
        {
            this.Hide();

        }

        //“退出”单击事件
        private void 退出_Click(object sender, EventArgs e)
        {
            //点击“是（Yes）”退出程序
            if (MessageBox.Show("确定要退出程序？", "安全提示",
                System.Windows.Forms.MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning)
                == System.Windows.Forms.DialogResult.Yes
                )
            {
                notifyIcon1.Visible = false; //设置图标不可见
                this.Close();              //关闭窗体
                this.Dispose();            //释放资源
                Application.Exit();        //关闭应用程序窗体
            }
        }

        //鼠标左键点击图标事件
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            //点击鼠标“左键”
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;                        //窗体可见
                this.WindowState = FormWindowState.Normal;  //窗体默认大小
                this.notifyIcon1.Visible = true;           //设置图标可见
            }
        }

        //鼠标左键双击图标事件
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show(); // 恢复窗口
            this.WindowState = FormWindowState.Normal; // 窗口状态设置为正常
            notifyIcon1.Visible = false; // 隐藏系统托盘图标
        }

        //窗体关闭前发生事件
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //窗体关闭原因为单击“关闭”按钮或Alt+F4
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true; //取消关闭操作 
                this.Hide();     //隐藏窗体
                this.notifyIcon1.Visible = true;           //设置图标可见
            }
        }

        private void textBoxDay_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许输入数字（0-9）、退格键和退出键
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                // 如果输入非数字，则取消输入
                e.Handled = true;
            }
        }

        private void textBoxHour_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许输入数字（0-9）、退格键和退出键
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                // 如果输入非数字，则取消输入
                e.Handled = true;
            }
        }

        private void textBoxMinute_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许输入数字（0-9）、退格键和退出键
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                // 如果输入非数字，则取消输入
                e.Handled = true;
            }
        }

        private void textBoxSecond_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许输入数字（0-9）、退格键和退出键
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                // 如果输入非数字，则取消输入
                e.Handled = true;
            }
        }

        private void delOldDays_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许输入数字（0-9）、退格键和退出键
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                // 如果输入非数字，则取消输入
                e.Handled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PerformTasks();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            if (everyDayCheck1.Checked)
            {
                if (now.Hour == everyDayTime1.Value.Hour & now.Minute == everyDayTime1.Value.Minute)
                {
                    PerformTasks();
                }
            }
            if (everyDayCheck2.Checked)
            {
                if (now.Hour == everyDayTime2.Value.Hour & now.Minute == everyDayTime2.Value.Minute)
                {
                    PerformTasks();
                }
            }
            if (everyDayCheck3.Checked)
            {
                if (now.Hour == everyDayTime3.Value.Hour & now.Minute == everyDayTime3.Value.Minute)
                {
                    PerformTasks();
                }
            }
            if (everyDayCheck4.Checked)
            {
                if (now.Hour == everyDayTime4.Value.Hour & now.Minute == everyDayTime4.Value.Minute)
                {
                    PerformTasks();
                }
            }
            if (everyDayCheck5.Checked)
            {
                if (now.Hour == everyDayTime5.Value.Hour & now.Minute == everyDayTime5.Value.Minute)
                {
                    PerformTasks();
                }
            }

        }

        private void saveset_Click(object sender, EventArgs e)
        {
            var appSet = new AppSet
            {
                TextBox1 = textBox1.Text,
                TextBox2 = textBox2.Text,
                WorkMode = workMode.Text,
                OnlyExtensions = onlyExtensions.Checked,
                OnlyExtensionsText = onlyExtensionsText.Text,
                IncludeSubfolders = includeSubfolders.Checked,
                ZipFileCheck = zipFileCheck.Checked,
                ZipFormat = zipFormat.Text,
                DeleteOldFileCheck = deleteOldFileCheck.Checked,
                DelOldDays = delOldDays.Text,
                ExecuteRadioButton = executeRadioButton.Checked,
                IntervalRadioButton = intervalRadioButton.Checked,
                EverydayRadioButton = everydayRadioButton.Checked,
                TextBoxDay = textBoxDay.Text,
                TextBoxHour = textBoxHour.Text,
                TextBoxMinute = textBoxMinute.Text,
                TextBoxSecond = textBoxSecond.Text,
                EveryDayCheck1 = everyDayCheck1.Checked,
                EveryDayCheck2 = everyDayCheck2.Checked,
                EveryDayCheck3 = everyDayCheck3.Checked,
                EveryDayCheck4 = everyDayCheck4.Checked,
                EveryDayCheck5 = everyDayCheck5.Checked,
                EveryDayTime1 = everyDayTime1.Value,
                EveryDayTime2 = everyDayTime2.Value,
                EveryDayTime3 = everyDayTime3.Value,
                EveryDayTime4 = everyDayTime4.Value,
                EveryDayTime5 = everyDayTime5.Value,

            };

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "设置文件 (*.json)|*.json|所有文件 (*.*)|*.*"; // 设置文件过滤器
            saveFileDialog.Title = "保存文件"; // 设置对话框标题
            saveFileDialog.FileName = "Setting"; // 设置默认文件名

            if (saveFileDialog.ShowDialog() == DialogResult.OK) // 显示对话框并检查用户是否点击了“确定”
            {
                string json = JsonConvert.SerializeObject(appSet, Formatting.Indented); // 格式化输出‌
                string filePath = saveFileDialog.FileName; // 获取选中的文件路径和文件名
                File.WriteAllText(filePath, json); // 写入内容到文件

            }


        }

        private void loadset_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "设置文件 (*.json)|*.json|所有文件 (*.*)|*.*"; // 设置文件过滤器
            openFileDialog.Title = "打开文件"; // 设置对话框标题

            if (openFileDialog.ShowDialog() == DialogResult.OK) // 显示对话框并检查用户是否点击了“确定”
            {
                string filePath = openFileDialog.FileName; // 获取选中的文件路径
                string jsonContent = File.ReadAllText(filePath); // 读取文件‌:
                AppSet appSetData = JsonConvert.DeserializeObject<AppSet>(jsonContent); // 反序列化‌

                textBox1.Text = appSetData.TextBox1;
                textBox2.Text = appSetData.TextBox2;
                workMode.Text = appSetData.WorkMode;
                onlyExtensions.Checked = appSetData.OnlyExtensions;
                onlyExtensionsText.Text = appSetData.OnlyExtensionsText;
                includeSubfolders.Checked = appSetData.IncludeSubfolders;
                zipFileCheck.Checked = appSetData.ZipFileCheck;
                zipFormat.Text = appSetData.ZipFormat;
                deleteOldFileCheck.Checked = appSetData.DeleteOldFileCheck;
                delOldDays.Text = appSetData.DelOldDays;
                executeRadioButton.Checked = appSetData.ExecuteRadioButton;
                intervalRadioButton.Checked = appSetData.IntervalRadioButton;
                everydayRadioButton.Checked = appSetData.EverydayRadioButton;
                textBoxDay.Text = appSetData.TextBoxDay;
                textBoxHour.Text = appSetData.TextBoxHour;
                textBoxMinute.Text = appSetData.TextBoxMinute;
                textBoxSecond.Text = appSetData.TextBoxSecond;
                everyDayCheck1.Checked = appSetData.EveryDayCheck1;
                everyDayCheck2.Checked = appSetData.EveryDayCheck2;
                everyDayCheck3.Checked = appSetData.EveryDayCheck3;
                everyDayCheck4.Checked = appSetData.EveryDayCheck4;
                everyDayCheck5.Checked = appSetData.EveryDayCheck5;
                everyDayTime1.Value = appSetData.EveryDayTime1;
                everyDayTime2.Value = appSetData.EveryDayTime2;
                everyDayTime3.Value = appSetData.EveryDayTime3;
                everyDayTime4.Value = appSetData.EveryDayTime4;
                everyDayTime5.Value = appSetData.EveryDayTime5;


            }
        }
    }
}
